package com.wx.spider.pc.controller;

import com.wx.spider.common.model.CommonResponse;
import com.wx.spider.pc.config.CloudConfig;
import com.wx.spider.pc.feign.JavaWsClient;
import com.wx.spider.pc.repository.WxNewsListJsonRepository;
import com.wx.spider.redis.util.RedisUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private CloudConfig cloudConfig;

    @Autowired
    private JavaWsClient javaWsClient;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private WxNewsListJsonRepository wxNewsListJsonRepository;

    @CrossOrigin(origins = "*",maxAge = 3600)
    @RequestMapping(value = "/mongo")
    public CommonResponse<Long> mongo() {
        try {
            long count = wxNewsListJsonRepository.count();
            return CommonResponse.succeed(count);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResponse.INTERNAL_ERROR_RESPONSE;
        }
    }


    @CrossOrigin(origins = "*",maxAge = 3600)
    @RequestMapping(value = "/hi")
    public String hi() {

        System.out.println("请求进来，在锁外边！");

        RLock lock = redissonClient.getLock("anyLock");
        lock.lock();



        String hi = null;
        try {

            hi = javaWsClient.hi();

            Object name = redisUtil.get("name");
            System.out.println(name);

//            boolean flag = redisUtil.set("name", "redis starter");
//            System.out.println(flag);
//            name = redisUtil.get("name");
//            System.out.println(name);
//            System.out.println((String)name);

//            System.out.println("==" + stringRedisTemplate.opsForValue().get("name"));
//        stringRedisTemplate.opsForValue().set("name1", "sr");
//            System.out.println("==" + stringRedisTemplate.opsForValue().get("name1"));
//            redisTemplate.opsForValue().set("name2", "r");
//            System.out.println("==" + redisTemplate.opsForValue().get("name2"));
        } catch (Exception e){
            e.printStackTrace();
        }finally {


            if(lock.isLocked()){ // 是否还是锁定状态
                if(lock.isHeldByCurrentThread()){ // 是否是当前执行线程的锁
                    lock.unlock(); // 释放锁
                }else{
                    System.out.println("不是自己的线程锁不释放");
                }
            }else{
                System.out.println("被其他线程释放了");
            }
        }

        return cloudConfig.getPort() + "_" + hi;
    }
}
